<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd">添加管家</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">姓名：</span>
        <el-input placeholder="请输入姓名" v-model="searchParams.name" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">手机号：</span>
        <el-input placeholder="请输入手机号" oninput="value=value.replace(/[^\d]/g,'')" v-model="searchParams.tel"
                  @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">门店：</span>
        <el-select v-model="searchParams.shop_id" placeholder="请选择门店" clearable>
          <el-option :label="item.shop_title" :value="item.shop_id" v-for="(item, index) in selectOptions.shop_id"
                     :key="index"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">是否离职：</span>
        <el-select v-model="searchParams.is_leave" placeholder="请选择是否离职" clearable>
          <el-option label="是" :value="1"/>
          <el-option label="否" :value="2"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">创建时间：</span>
        <el-date-picker
            v-model="searchParamsTime"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
        />
      </div>

      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="name" label="基础信息">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="姓名">{{ row.name }}</el-descriptions-item>
            <el-descriptions-item label="手机号">{{ row.tel }}</el-descriptions-item>
            <el-descriptions-item label="身份证号">{{ row.id_card_no }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="company_name" label="公司">
        <template #default="{ row }">
          <div style="display: flex;align-items: center;">
            <el-image style="width: 50px; height: 50px;margin-right: 10px" :src="proxy.imgUrl + row.company_logo"
                      fit="cover"/>
            <el-descriptions :column="1">
              <el-descriptions-item label="名称">{{ row.company_name }}</el-descriptions-item>
              <el-descriptions-item label="职务">{{ row.job }}</el-descriptions-item>
            </el-descriptions>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="bank_no" label="银行">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="开户银行">{{ row.bank_name }}</el-descriptions-item>
            <el-descriptions-item label="银行卡号">{{ row.bank_no }}</el-descriptions-item>
            <el-descriptions-item label="预留手机号">{{ row.bank_tel }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="shop_title" label="门店"></el-table-column>
      <el-table-column prop="status" label="审核">
        <template #default="{ row }">
          <el-descriptions :column="1">
            <el-descriptions-item label="状态">
              <el-text v-if="row.status==1" type="warning">待审核</el-text>
              <el-text v-if="row.status==2" type="success">已通过</el-text>
              <el-text v-if="row.status==3" type="danger">未通过</el-text>
            </el-descriptions-item>
            <el-descriptions-item label="审核意见">{{ row.check_cause }}</el-descriptions-item>
            <el-descriptions-item label="审核时间">{{ row.check_time }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column prop="is_leave" label="是否离职" width="100">
        <template #default="{ row }">
          <el-radio-group v-model="row.is_leave" size="small" @change="is_leave_change(row,$event)">
            <el-radio-button :label="1">是</el-radio-button>
            <el-radio-button :label="2">否</el-radio-button>
          </el-radio-group>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{guanjia_id:row.guanjia_id})">编辑</el-button>
          <el-button link type="primary" @click="handleCheck(row)">审核</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px"
               :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">
        <el-form-item label="姓名" prop="name" required>
          <el-input v-model.trim="dialogForm.name" clearable placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="手机号" prop="tel" required>
          <el-input v-model.trim="dialogForm.tel" clearable placeholder="请输入手机号"
                    oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input v-model.trim="dialogForm.password" clearable placeholder="请输入密码"
                    oninput="value=value.replace(/[^\d]/g,'')" style="width: 50%"/>
          <el-text type="danger" style="margin-left: 5px">格式：6位数字组成</el-text>
        </el-form-item>
        <el-form-item label="门店" prop="shop_id" required>
          <el-select v-model="dialogForm.shop_id" placeholder="请选择门店">
            <el-option :label="item.shop_title" :value="item.shop_id" v-for="(item, index) in selectOptions.shop_id"
                       :key="index"/>
          </el-select>
        </el-form-item>
        <el-form-item label="管理小区" prop="min_area_id" required>
          <el-select multiple v-model="dialogForm.min_area_id" placeholder="请选择小区">
            <el-option :label="item.min_area_title" :value="item.min_area_id"
                       v-for="(item, index) in selectOptions.min_area_id"
                       :key="index"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog v-model="checkDialogShow" title="审核" width="660px"
               :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeCheckDialog">
      <el-form ref="checkDialogFormRef" :model="checkDialogForm" :rules="checkDialogFormRules" label-width="120px">
        <el-form-item label="审核结果" prop="status" required>
          <el-radio-group v-model="checkDialogForm.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="check_cause" required>
          <el-input v-model.trim="checkDialogForm.check_cause" clearable placeholder="请输入审核意见" :rows="2"
                    type="textarea"/>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="checkDialogParams.loading"
                       @click="submitCheckDialogForm(checkDialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(checkDialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import chinaAreaData from "china-area-data";
import {validPhone} from "@common/utils";

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  resetDialogForm,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

dialogForm.name = '';
dialogForm.tel = '';
dialogForm.password = '';
dialogForm.shop_id = '';
dialogForm.min_area_id = [];

const router = useRouter()
const route = useRoute()

apiPath.value = 'admin/guanJia/guanJia/index';
dialogParams.addApiPath = 'admin/guanJia/guanJia/create';
dialogParams.editApiPath = 'admin/guanJia/guanJia/update';

const checkDialogShow = ref(false)
const checkDialogForm = reactive({
  guanjia_id: '',
  status: 2,
  check_cause: ''
})
const checkDialogFormRef = ref(null)
const checkDialogParams = reactive({
  loading: false
})
const searchParamsTime = ref('');
watch(searchParamsTime, (newV, oldV) => {
  if (!newV) {
    searchParams.s_time = ''
    searchParams.e_time = ''
  } else {
    searchParams.s_time = newV[0]
    searchParams.e_time = newV[1]
  }
})
onMounted(() => {
  getData()
  getShopIdSelect()
  getMinAreaIdSelect()
})

const dialogFormRules = reactive({
  name: [{
    required: true,
    message: "请输入管家名称",
    trigger: "blur"
  }],
  tel: [{
    required: true,
    message: "请输入管家手机号",
    trigger: "blur"
  },{
    validator: proxy.$util.validPhone,
    trigger: "blur"
  }],
  password: [{
    required: true,
    message: "请输入密码",
    trigger: "blur"
  }],
  shop_id: [{
    required: true,
    message: "请选择门店",
    trigger: "change"
  }],
  min_area_id:[{
    required: true,
    message: "请选择管理小区",
    trigger: "change"
  }]
})
const checkDialogFormRules = reactive({
  status: [{
    required: true,
    message: "请选择审核结果",
    trigger: "change"
  }],
  check_cause: [{
    required: true,
    message: "请输入审核意见",
    trigger: "blur"
  }]
})
const handleCheck = (row) => {
  checkDialogShow.value = true
  checkDialogForm.guanjia_id = row.guanjia_id
  checkDialogForm.status = 2
  checkDialogForm.check_cause = ''
}
const closeCheckDialog = () => {
  checkDialogShow.value = false
  resetDialogForm(checkDialogFormRef.value)
}
const submitCheckDialogForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      checkDialogParams.loading = true
      let data = checkDialogForm;
      let url = 'admin/guanJia/guanJia/doCheck';
      proxy.$axios.post(url, data)
          .then((res) => {
            checkDialogParams.loading = false
            if (res.data.code == 200) {
              closeCheckDialog()
              proxy.$message({
                message: `成功`,
                type: "success"
              })
              getData()
            } else {
              proxy.$message({
                message: res.data.msg,
                type: "error"
              })
            }
          })
          .catch((err) => {
            checkDialogParams.loading = false
          })
    }
  })
}

const handleEdit = async (row, data) => {
  dialogShow.value = true
  dialogParams.type = 'edit'
  dialogParams.data = data
  setTimeout(() => {
    for (let key in dialogForm) {
      dialogForm[key] = row[key]
    }
    dialogForm.min_area_id = [];
    proxy.$axios
        .get("admin/guanJia/guanJia/minAreaIndex", {params: {guanjia_id: row.guanjia_id, limit: 1000}})
        .then((res) => {
          if (res.data.code == 200) {
            console.log(res.data.data);
            res.data.data.data.forEach(item => {
              dialogForm.min_area_id.push(item.min_area_id)
            })
          } else {
            proxy.$message({
              message: res.data.msg,
              type: "error"
            })
          }
        })
        .catch((err) => {
        })
  }, 50)
}

const handleDelete = (row) => {
  handleAction('admin/guanJia/guanJia/delete', {
    guanjia_id: row.guanjia_id
  })
}

const is_leave_change = (row, e) => {
  handleAction('admin/guanJia/guanJia/leave', {
    guanjia_id: row.guanjia_id,
    is_leave: e
  })
}

const selectOptions = reactive({
  shop_id: [],
  min_area_id: []
})
const getShopIdSelect = () => {
  proxy.$axios
      .get("common/dataSelect/shopSelect")
      .then((res) => {
        if (res.data.code == 200) {
          selectOptions.shop_id = res.data.data
        } else {
          proxy.$message({
            message: res.data.msg,
            type: "error"
          })
        }
      })
      .catch((err) => {
      })
}
const getMinAreaIdSelect = () => {
  proxy.$axios
      .get("common/dataSelect/minAreaSelect")
      .then((res) => {
        if (res.data.code == 200) {
          selectOptions.min_area_id = res.data.data
        } else {
          proxy.$message({
            message: res.data.msg,
            type: "error"
          })
        }
      })
      .catch((err) => {
      })
}


</script>

<style lang="less">
.el-descriptions__cell {
  padding-bottom: 0 !important;
}

.el-descriptions__label {
  color: #999999 !important
}

</style>